# coding: utf-8

from flask import request, current_app, render_template, flash, redirect, url_for
from flask_login import login_required

from app.enterprise import enterprise_bp
from app.extensions import db
from app.enterprise.models import HpProjectType, HpSecuritySystem, HpProductType
from app.enterprise.forms import ProductBaseTypeForm, ProjectBaseTypeForm, SecurityBaseTypeForm


@enterprise_bp.route('/project_type_list')
@login_required
def project_type_list():
    query = HpProjectType.query
    page = request.args.get('page', 1, type=int)
    pagination = query.order_by(
        HpProjectType.id
    ).paginate(page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
               error_out=False)
    items = pagination.items
    return render_template(
        'project_type_list.html',
        page=page,
        pagination=pagination,
        items=items
    )


@enterprise_bp.route('/project_type_edit/<int:type_id>', methods=['GET', 'POST'])
@login_required
def project_type_edit(type_id):
    form = ProjectBaseTypeForm()

    new_project_type_edit = HpProjectType.query.get_or_404(type_id)
    if form.validate_on_submit():
        if HpProjectType.query.filter(
            HpProjectType.id != type_id,
            HpProjectType.name == form.name.data
        ).first():
            flash('该类型已存在')
            return redirect(url_for('enterprise_bp.project_type_edit', type_id=type_id))
        form.populate_obj(new_project_type_edit)
        flash('修改成功')
        return redirect(url_for('enterprise_bp.project_type_list'))

    form = ProjectBaseTypeForm(obj=new_project_type_edit)
    return render_template(
        'project_type_edit.html',
        form=form,
        new_project_type_edit=new_project_type_edit
    )


@enterprise_bp.route('/project_type_add', methods=['GET', 'POST'])
@login_required
def project_type_add():
    form = ProjectBaseTypeForm()

    if form.validate_on_submit():
        if HpProjectType.query.filter(
            HpProjectType.name == form.name.data
        ).first():
            flash('该类型已存在')
            return redirect(url_for('enterprise_bp.project_type_add'))
        new_porject_type_set = HpProjectType()
        form.populate_obj(new_porject_type_set)
        db.session.add(new_porject_type_set)
        db.session.flush()
        flash('新增成功')
        return redirect(url_for('enterprise_bp.project_type_list'))

    else:
        if form.errors:
            flash('表单数据有误，请检查')
            print(form.errors)
    return render_template(
        'project_type_add.html',
        form=form,
    )


@enterprise_bp.route('/security_system_list')
@login_required
def security_system_list():
    query = HpSecuritySystem.query
    page = request.args.get('page', 1, type=int)
    pagination = query.order_by(
        HpSecuritySystem.id
    ).paginate(page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
               error_out=False)
    items = pagination.items
    return render_template(
        'security_system_list.html',
        page=page,
        pagination=pagination,
        items=items
    )


@enterprise_bp.route('/security_system_add', methods=['GET', 'POST'])
@login_required
def security_system_add():
    form = SecurityBaseTypeForm()

    if form.validate_on_submit():
        if HpSecuritySystem.query.filter(
            HpSecuritySystem.name == form.name.data
        ).first():
            flash('该保障体系类型已存在')
            return redirect(url_for('enterprise_bp.security_system_add'))
        new_security_system_list = HpSecuritySystem()
        form.populate_obj(new_security_system_list)
        db.session.add(new_security_system_list)
        db.session.flush()
        flash('新增成功')
        return redirect(url_for('enterprise_bp.security_system_list'))

    else:
        if form.errors:
            flash('表单数据有误，请检查')
            print(form.errors)
    return render_template(
        'security_system_add.html',
        form=form,
    )


@enterprise_bp.route('/security_system_edit/<int:system_id>', methods=['GET', 'POST'])
@login_required
def security_system_edit(system_id):
    form = SecurityBaseTypeForm()
    security_system_set = HpSecuritySystem.query.get_or_404(system_id)
    if form.validate_on_submit():
        if HpSecuritySystem.query.filter(
            HpSecuritySystem.id != system_id,
            HpSecuritySystem.name == form.name.data
        ).first():
            flash('保障体系类型已存在')
            return redirect(
                url_for('enterprise_bp.security_system_edit', system_id=system_id)
            )
        form.populate_obj(security_system_set)
        flash('修改成功')
        return redirect(url_for('enterprise_bp.security_system_list'))
    form = SecurityBaseTypeForm(obj=security_system_set)
    return render_template(
        'security_system_edit.html',
        security_system_set=security_system_set,
        form=form
    )


@enterprise_bp.route('/product_type_list')
@login_required
def product_type_list():
    query = HpProductType.query
    page = request.args.get('page', 1, type=int)
    pagination = query.order_by(
        HpProductType.id
    ).paginate(page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
               error_out=False)
    items = pagination.items

    return render_template(
        'product_type_list.html',
        page=page,
        pagination=pagination,
        items=items
    )


@enterprise_bp.route('/product_type_add', methods=['POST', 'GET'])
@login_required
def product_type_add():
    form = ProductBaseTypeForm()

    if form.validate_on_submit():
        if HpProductType.query.filter(
            HpProductType.name == form.name.data
        ).first():
            flash('产品类型已存在')
            return render_template(url_for('enterprise_bp.product_type_add'))

        new_product_type_list = HpProductType()
        form.populate_obj(new_product_type_list)
        db.session.add(new_product_type_list)
        db.session.flush()
        flash('新增成功')
        return redirect(url_for('enterprise_bp.product_type_list'))

    else:
        if form.errors:
            flash('表单数据有误，请检查')
            print(form.errors)
    return render_template(
        'product_type_add.html',
        form=form,
    )


@enterprise_bp.route('/product_type_edit/<int:type_id>', methods=['POST', 'GET'])
@login_required
def product_type_edit(type_id):
    form = ProductBaseTypeForm()
    product_type_set = HpProductType.query.get_or_404(type_id)
    if form.validate_on_submit():
        if HpProductType.query.filter(
            HpProductType.id != type_id,
            HpProductType.name == form.name.data
        ).first():
            flash('产品类型已存在')
            return redirect(
                url_for('enterprise_bp.product_type_edit', type_id=type_id)
            )
        form.populate_obj(product_type_set)
        flash('修改成功')
        return redirect(url_for('enterprise_bp.product_type_list'))
    form = ProductBaseTypeForm(obj=product_type_set)
    return render_template(
        'product_type_edit.html',
        product_type_set=product_type_set,
        form=form
    )
